package com.maxmlr.music.system.task;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.maxmlr.music.system.dao.TaskMapper;
import com.maxmlr.music.system.entity.Task;
import com.maxmlr.music.system.task.factory.QuartzManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;

import java.util.List;

/**
 * @ClassName StartJob
 * @Description TODO
 * @Author maxiao
 * @Date 2020/8/26 下午3:50
 * @Version V1.0
 */
@Configuration
public class StartJob implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    private TaskMapper taskMapper;

    @Autowired
    private QuartzManager quartzManager;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        System.out.println("启动task");
        init();
    }

    public void init() {
        // 这里获取任务信息数据
        List<Task> taskList = taskMapper.selectList(Wrappers.<Task>lambdaQuery().eq(Task::getJobStatus, "1"));
        for (Task task : taskList) {
            quartzManager.addJob(task);
        }
    }
}
